<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\House;
use app\util\Util;
use app\util\File;

/*
 * 房抵贷
 */
class HouseController extends CommonController
{
    /*
     * 房抵贷列表
     */
    public function index()
    {
        $list = House::field('description, img', true)
            ->order('add_time desc')
            ->paginate(15);

        return view('index', [
            'list' => $list
        ]);
    }

    /*
     * 添加房抵贷
     */
    public function add()
    {
        return view('add');
    }

    /*
     * 写入房抵贷
     */
    public function postInsert()
    {
        $all = input('post.');

        $name = $all['name'];
        $description = $all['description'];
        $money = $all['money'];
        $month = $all['month'];
        $lixi = $all['lixi'];
        $speed = $all['speed'];
        $payment = $all['payment'];
        $img = $all['img'];
        $content = $all['content'];

        $moneyArray = explode('-', $money);
        $money_min = $moneyArray[0];
        $money_max = $moneyArray[1];

        $monthArray = explode('-', $month);
        $month_min = $monthArray[0];
        $month_max = $monthArray[1];

        $speedArray = explode('-', $speed);
        $speed_min = $speedArray[0];
        $speed_max = $speedArray[1];

        $data = [
            'name' => $name,
            'description' => $description,
            'money_min' => $money_min,
            'money_max' => $money_max,
            'month_min' => $month_min,
            'month_max' => $month_max,
            'lixi' => $lixi,
            'speed_min' => $speed_min,
            'speed_max' => $speed_max,
            'img' => $img,
            'payment' => $payment,
            'content' => $content,
            'add_time' => time(),
        ];

        $result = House::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
     * 编辑房抵贷
     */
    public function edit()
    {
        $id = input('param.id');

        $row = House::where('id', '=', $id)->find();

        return view('edit', [
            'id' => $id,
            'row' => $row,
        ]);
    }

    /*
     * 保存房抵贷
     */
    public function postSave()
    {
        $all = input('post.');

        $id = $all['id'];
        $name = $all['name'];
        $description = $all['description'];
        $money = $all['money'];
        $month = $all['month'];
        $lixi = $all['lixi'];
        $speed = $all['speed'];
        $payment = $all['payment'];
        $img = $all['img'];
        $content = $all['content'];

        $moneyArray = explode('-', $money);
        $money_min = $moneyArray[0];
        $money_max = $moneyArray[1];

        $monthArray = explode('-', $month);
        $month_min = $monthArray[0];
        $month_max = $monthArray[1];

        $speedArray = explode('-', $speed);
        $speed_min = $speedArray[0];
        $speed_max = $speedArray[1];

        $data = [
            'name' => $name,
            'description' => $description,
            'money_min' => $money_min,
            'money_max' => $money_max,
            'month_min' => $month_min,
            'month_max' => $month_max,
            'lixi' => $lixi,
            'speed_min' => $speed_min,
            'speed_max' => $speed_max,
            'payment' => $payment,
            'content' => $content,
            'add_time' => time(),
        ];

        $row = null;
        if ($img != '') {
            $row = House::where('id', '=', $id)->field('img')->find();

            $data['img'] = $img;
        }

        $result = House::where('id','=', $id)->update($data);
        if ($result) {
            if ($img != '') {
                $file = new File();
                $file->deleteImage($row['img']);
            }

            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
     * 删除房抵贷
     */
    public function del()
    {
        $id = input('param.id');

        $row = House::where('id', '=', $id)->field('img')->find();

        $result = House::where('id', '=', $id)->delete();
        if ($result) {
            $file = new File();
            $file->deleteImage($row['img']);

            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }

}